<template>
  <div>
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body :before-close="close">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="demo-ruleForm">
        <el-row>
          <el-col :span="8">
            <el-form-item label="工作号" prop="job_number">
              <el-select v-model="form.job_number" placeholder="" clearable style="width: 100%">
                <el-option
                  v-for="(item, index) in job_number_data"
                  :key="index"
                  :label="item"
                  :value="item"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="报关单号" prop="customs_number">
              <el-input v-model="form.customs_number" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="上传报关单">
            <el-upload
              ref="upload"
              class="upload-demo"
              :headers="importHeaders"
              :action="url.import"
              :on-success="handleSuccess"
              :on-change="handleImport"
              :file-list="fileList"
              :multiple="false"
              :limit="1"
              accept=".csv, application/pdf, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
            >
              <el-button size="mini" type="success">选择文件</el-button>
              <div slot="tip" class="el-upload__tip">只能上传csv/xlsx/xls/pdf文件</div>
            </el-upload>
          </el-form-item>
          <el-form-item label="上传报关资料">
            <el-upload
              ref="upload"
              class="upload-demo"
              :headers="importHeaders"
              :action="url.import"
              :on-success="handleSuccess2"
              :on-change="handleImport"
              :file-list="fileList2"
              :multiple="false"
              :limit="1"
              accept=".csv, application/pdf, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
            >
              <el-button size="mini" type="success">选择文件</el-button>
              <div slot="tip" class="el-upload__tip">只能上传csv/xlsx/xls/pdf文件</div>
            </el-upload>
          </el-form-item>
        </el-row>

        <el-row style="top: 30px;margin-bottom: 30px;text-align:center;">
          <el-col :span="24">
            <el-button type="primary" size="medium" @click="submitForm('form')">{{ title }}</el-button>
            <el-button size="medium" @click="close()">取消</el-button>
          </el-col>
        </el-row>

      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { reportCreate, reportEdit } from '@/api/report'
import { getJobNumber } from '@/api/business'
import { mapGetters } from 'vuex'
export default {
  name: 'AddEdit',
  props: {
    open: {
      type: Boolean
    },
    form: {
      type: Object
    },
    title: {
      type: String
    }
  },
  data() {
    return {
      rules: {
        job_number: [
          { required: true, message: '请选择工作号', trigger: 'change' }
        ],
        customs_number: [
          { required: true, message: '请填写报关单号', trigger: 'blur' }
        ]
      },
      // 上传头部
      importHeaders: { authorization: 'Bearer ' + this.token },
      // 上传地址
      url: {
        import: process.env.VUE_APP_BASE_API + '/upload/file'
      },
      // 上传列表
      fileList: [],
      fileList2: [],
      job_number_data: []
    }
  },
  computed: {
    ...mapGetters([
      'token'
    ])
  },
  watch: {
    'form': function(newValue, oldValue) {
      if (JSON.stringify(this.form) !== '{}' && this.title === '编辑') {
        this.fileList = []
        this.fileList2 = []
        // 报关单
        // 上传列表
        this.fileList = [{
          'name': this.form.file_path.split('/').pop(),
          'url': this.form.file_path
        }]
        this.fileList2 = [{
          'name': this.form.file_path_two.split('/').pop(),
          'url': this.form.file_path_two
        }]
      } else if (JSON.stringify(this.form) === '{}' || this.title === '添加') {
        this.fileList = []
        this.fileList2 = []
      }
    }
  },
  created() {
    this.JobNumber()
  },
  methods: {
    JobNumber() {
      getJobNumber().then(response => {
        if (response.code === 10000) {
          this.job_number_data = response.data
        }
      })
    },
    submitForm(formName) {
      const that = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (that.title === '添加') {
            this.add()
          } else {
            this.edit()
          }
        } else {
          return false
        }
      })
    },
    add() {
      const that = this
      reportCreate(that.form).then(response => {
        if (response.code === 10000) {
          that.$message.success(response.message)
          that.$parent.getList()
          that.$emit('update:open', false)
        }
      })
    },
    edit() {
      const that = this
      reportEdit(that.form).then(response => {
        if (response.code === 10000) {
          that.$message.success(response.message)
          that.$parent.getList()
          that.$emit('update:open', false)
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    close() {
      this.$emit('update:open', false)
    },
    // 打开导入弹窗
    handleImport() {
      this.importHeaders.authorization = 'Bearer ' + this.token
      this.url.import = process.env.VUE_APP_BASE_API + '/upload/file'
    },
    // 报关单成功提示
    handleSuccess(response, file, fileList) {
      if (response.code === 10000) {
        this.form.file_path = response.data
      } else {
        this.$message.error(response.message)
      }
    },
    // 报关单资料成功提示
    handleSuccess2(response, file, fileList) {
      if (response.code === 10000) {
        this.form.file_path_two = response.data
      } else {
        this.$message.error(response.message)
      }
    }
  }
}
</script>
<style>

.demo-table-expand label {
  width: 40%;
}

</style>

